<style lang="less">
    @import "corporateTransferForBusiness.less";
</style>
<template>
    <div>
        <Card>
            <Row>
                <Form  ref="searchForm" :model="searchForm" inline :label-width="90" class="search">
                    <div class="search">
                        <Row>
                            <Col align="left">
                                <Form-item label="申请编号" prop="caseNo">
                                    <Input type="text" v-model="searchForm.applyNo" clearable placeholder="请输入申请编号"/>
                                </Form-item>
                                <Form-item label="客户姓名" prop="custName">
                                    <Input type="text" v-model="searchForm.custName" clearable placeholder="请输入客户姓名"/>
                                </Form-item>
                                <Form-item label="合同号" prop="contractNo">
                                    <Input type="text" v-model="searchForm.contractNo" clearable placeholder="请输入合同号"/>
                                </Form-item>
                                <Form-item label="来源" prop="caseSource">
                                    <Select v-model="searchForm.caseSource"  clearable placeholder="请选择来源">
                                        <Option
                                            v-for="(item,index) in dataDict.businessSourceDict"
                                            :key="index"
                                            :value="item.value"
                                        >{{item.title}}</Option>
                                    </Select>
                                </Form-item>
                                <Form-item label="到账银行" prop="receivingBank">
                                    <Select v-model="searchForm.receivingBank"  clearable placeholder="请选择到账银行">
                                        <Option
                                            v-for="(item,index) in dataDict.repaymentBankDict"
                                            :key="index"
                                            :value="item.value"
                                        >{{item.title}}</Option>
                                    </Select>
                                </Form-item>
                                <Form-item label="处理状态" prop="caseStatus">
                                    <Select v-model="searchForm.caseStatus"  clearable placeholder="请选择处理状态">
                                        <Option
                                            v-for="(item,index) in dataDict.caseStatusDict"
                                            :key="index"
                                            :value="item.value"
                                        >{{item.title}}</Option>
                                    </Select>
                                </Form-item>
                                <Form-item label="申请时间" prop="startDate">
                                    <DatePicker v-model="startDate" type="daterange" format="yyyy-MM-dd" clearable
                                                @on-change="startDateRange" placeholder="选择申请时间">
                                    </DatePicker>
                                </Form-item>
                                <Form-item label="结束时间" prop="endDate">
                                    <DatePicker v-model="endDate" type="daterange" format="yyyy-MM-dd" clearable
                                                @on-change="endDateRange" placeholder="选择结束时间">
                                    </DatePicker>
                                </Form-item>
                                <Form-item label="还款类型" prop="caseType">
                                    <Select v-model="searchForm.caseType"  clearable placeholder="请选择还款类型">
                                        <Option
                                            v-for="(item,index) in dataDict.caseTypeDict"
                                            :key="index"
                                            :value="item.value"
                                        >{{item.title}}</Option>
                                    </Select>
                                </Form-item>
                                <Form-item label="全部案件" prop="allCase">
                                    <i-switch v-model="searchForm.allCase" @on-change="getCorporateList" ></i-switch>
                                </Form-item>
                            </Col>
                        </Row>
                        <Row>
                            <Col align="middle">
                                <Form-item>
                                    <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
                                    <Button @click="handleReset" type="primary" icon="ios-trash">重置</Button>
                                </Form-item>
                            </Col>
                        </Row>
                        <Row  class="operation">
                            <Button @click="exportData" type="primary" icon="md-list-box">导出报表</Button>
                            <Button @click="addCorporateOfRent" type="primary" icon="md-add">月供收款</Button>
                            <Button @click="addCorporateOfBefore" type="primary" icon="md-add">提前结清收款</Button>
                        </Row>
                    </div>
                    <Row>
                        <Table :loading="loading" border :columns="columns" :data="data" sortable="custom"
                               @on-selection-change="showSelect" ref="table">
                            <template slot-scope="{ row, index }" slot="id">
                                <Button v-show="row.caseStatus == 'UNDER_REVIEW'"
                                        type="primary" size="small" @click="dealCorporate(row)">处理</Button>
                                <Button v-show="row.caseStatus == 'NOT_SUBMIT'"
                                        type="error" size="small" @click="deleteCorporate(row)">删除</Button>
                                <Button v-show="row.caseStatus == 'NOT_SUBMIT'"
                                        type="error" size="small" @click="editCorporate(row)">编辑</Button>
                            </template>
                            <template slot-scope="{ row, index }" slot="contractNo">
                                <span><a @click="showDetail(row)">{{row.contractNo}}</a></span>
                            </template>
                            <template slot-scope="{ row, index }" slot="caseStatus">
                                <span>{{getTitle('caseStatus',row.caseStatus)}}</span>
                            </template>
                            <template slot-scope="{ row, index }" slot="caseType">
                                <span>{{getTitle("caseType",row.caseType)}}</span>
                            </template>
                            <template slot-scope="{ row, index }" slot="receivingBank">
                                <span>{{getTitle("bankCode",row.receivingBank)}}</span>
                            </template>
                            <template slot-scope="{ row, index }" slot="caseSource">
                                <span>{{getTitle("businessSource",row.caseSource)}}</span>
                            </template>
                            <template slot-scope="{ row, index }" slot="payAmt">
                                <span>{{showDecimal(row)}}</span>
                            </template>
                        </Table>
                    </Row>
                    <Row type="flex" justify="end" class="page">
                        <Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize"
                              @on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10,20,50]"
                              size="small" show-total show-elevator show-sizer></Page>
                    </Row>
                </Form>
            </Row>
        </Card>
        <ActivatedCase
            :isSP="false"
            :getDataFn="getDataFn"
            :contractBusiness="contractBusiness"
            :handleBusiness="'corporateSettlementRent'"
            :dataDict="dataDict"
            :modal="modal"
            modalTitle="已激活案件列表"
            @showContractDetial="showContractDetailRent"
            @closeModal="closeModal"
        />

        <prepaymentCase
            :isSP="false"
            :getDataFn="getDataFn"
            :contractBusiness="contractBusiness"
            :handleBusiness="'corporateSettlementPrepayment'"
            :dataDict="dataDict"
            :modal="modalBefore"
            modalTitle="提前结清勾稽列表"
            @showContractDetial="showContractDetailBefore"
            @closeModal="closeModalBefore"
        />

    </div>
</template>

<script>
    import {getDictDataList, getTitleFromDataDict} from "_p/basic/assets/contract/js/utils";
    import {
        deleteCorporateTransfer,
        exportCorporateList,
        findActivatedContractList,
        queryCorporateList
    } from "_p/basic/api/contract/corporate/corporateTransferManagement";

    import ActivatedCase from "../common/activated-case/index";
    import prepaymentCase from "../common/settle-case/index";

    export default {
        name: "corporate-transfer-for-business",
        components:{
            ActivatedCase,
            prepaymentCase
        },
        data() {
            return {
                viewModel:false,
                dealModel:false,
                editModel:false,
                modalTitle:"",
                modalVisible:false,
                startDate: null,
                endDate: null,
                searchForm:{
                    caseNo:"",custName:"",contractNo:"",caseSource:"",
                    receivingBank:"",caseStatus:"",caseType:"",startTimeS:"",
                    startTimeE:"",endTimeS:"",endTimeE:"",allCase:false
                },
                data:[],
                dataForExport:[],
                exportReady:false,
                total: 0,
                dicKeys: [
                    "bankCode","caseStatus","caseType","businessSource","PersonRoleType","contractStatus","channelType","repaymentBank","validOrInvalid"
                ],
                dataDict:{},
                loading: false,
                columns: [
                    {title: "操作",key: "id",minWidth: 150,align: "center",slot: "id",fixed:"left",slot: "id"},
                    {title: "合同号",key: "contractNo",minWidth: 180,align: "center",slot: "contractNo"},
                    {title: "申请编号",key: "applyNo",minWidth: 160,align: "center"},
                    {title: "案件号",key: "caseNo",minWidth: 160,align: "center"},
                    {title: "客户姓名",key: "custName",minWidth: 150,align: "center"},
                    {title: "申请时间",key: "startTime",minWidth: 150,align: "center"},
                    {title: "处理状态",key: "caseStatus",minWidth: 150,align: "center",slot: "caseStatus"},
                    {title: "结束时间",key: "endTime",minWidth: 150,align: "center"},
                    {title: "还款类型",key: "caseType",minWidth: 150,align: "center",slot:"caseType"},
                    {title: "到账银行",key: "receivingBank",minWidth: 150,align: "center",slot:"receivingBank"},
                    {title: "打款时间",key: "payTime",minWidth: 150,align: "center"},
                    {title: "打款金额",key: "payAmt",slot: "payAmt",minWidth: 150,align: "center"},
                    {title: "打款人",key: "payer",minWidth: 150,align: "center"},
                    {title: "来源",key: "caseSource",minWidth: 150,align: "center",slot:"caseSource"},
                ],
                viewObj:{
                    caseNo: "",
                    contractNo: "",
                    id:"",
                    collectionType: "receivableRent",//收款类型：月供收款
                },
                contractBusiness: ["closeContract"],
                modal: false,
                modalBefore: false
            }
        },
        computed:{
            getDataFn: function () {
                return findActivatedContractList;
            },
            // getDataFnBefore: function () {
            //     return getApproveSuccessContract;
            // },
        },
        methods:{
            closeModal() {
                this.modal = false;
            },
            closeModalBefore() {
                this.modalBefore = false;
            },
            exportData (type) {
                this.dataForExport = [];
                exportCorporateList(this.searchForm).then(res => {
                    if (res.code === "0000") {
                        this.dataForExport = res.data;
                        this.dataForExport.forEach(line =>{
                            line.caseStatus = this.getTitle("caseStatus",line.caseStatus);
                            line.caseType = this.getTitle("caseType",line.caseType);
                            line.receivingBank = this.getTitle("bankCode",line.receivingBank);
                            line.caseSource = this.getTitle("businessSource",line.caseSource);
                        });
                        this.$refs.table.exportCsv({
                            filename: '对公勾稽任务列表',
                            columns: this.columns.filter((col, index) => index > 0),
                            data: this.dataForExport
                        });
                    }else{
                        this.$Message.error("导出失败");
                    }
                });
            },
            startDateRange(v) {
                if (v) {
                    this.searchForm.startTimeS = v[0];
                    this.searchForm.startTimeE = v[1];
                }
            },
            endDateRange(v) {
                if (v) {
                    this.searchForm.endTimeS = v[0];
                    this.searchForm.endTimeE = v[1];
                }
            },
            handleSearch() {
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getCorporateList();
            },
            handleReset() {
                this.$refs.searchForm.resetFields();
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.searchForm.applyNo = null;
                this.startDate = null;
                this.endDate = null;
                this.searchForm.startTimeS = "";
                this.searchForm.startTimeE = "";
                this.searchForm.endTimeS = "";
                this.searchForm.endTimeE = "";

                // 重新加载数据
                this.getCorporateList();
            },
            changePage(v) {
                this.searchForm.pageNumber = v;
                this.getCorporateList();
            },
            changePageSize(v) {
                this.searchForm.pageSize = v;
                this.getCorporateList();
            },
            initDataDic(){
                getDictDataList(this.dicKeys).then(res => {
                    this.dataDict = res;
                });
            },
            getTitle(type, val) {
                let data = this.dataDict[type + "Dict"] || [];
                return getTitleFromDataDict(data, val);
            },
            showSelect(e) {
                this.selectList = e;
                this.selectCount = e.length;
            },
            showDecimal(v){
                if (v.payAmt){
                    if (v.payAmt == 0){
                        return "0.00"
                    }else{
                        return v.payAmt==""?"0.00":v.payAmt.toFixed(2)
                    }
                }else{
                    return "0.00"
                }
            },
            showDetail(v){
                let params={};
                params.modalTitle = "案件详情";
                params.dealModel = false;
                params.editModel = false;
                params.viewModel = true;
                params.caseNo = v.caseNo;
                params.contractNo = v.contractNo;
                params.caseType = v.caseType;
                params.prepaymentApplyId =  v.prepaymentApplyId;
                params.id = v.id;
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/corporate/index",
                    "对公收款详情",
                    "ios-add",
                    params,
                    "corporate-detial-" + v.contractNo,
                    [],
                    true
                );
            },
            dealCorporate(v){
                let params={};
                params.viewModel = false;
                params.editModel = false;
                params.dealModel = true;
                params.caseNo = v.caseNo;
                params.contractNo = v.contractNo;
                params.caseType = v.caseType;
                params.prepaymentApplyId =  v.prepaymentApplyId;
                params.id = v.id;
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/corporate/index",
                    "对公收款审批",
                    "ios-add",
                    params,
                    "corporate-detial-" + v.contractNo,
                    [],
                    true
                );
            },
            addCorporateOfRent(v){
                this.contractBusiness = ["closeContract",];
                this.modal = true;
            },
            editCorporate(v){
                let params={};
                params.viewModel = false;
                params.dealModel = false;
                params.editModel = true;
                params.caseType = v.caseType;
                params.caseNo = v.caseNo;
                params.contractNo = v.contractNo;
                params.id = v.id;
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/corporate/index",
                    "对公收款编辑",
                    "ios-add",
                    params,
                    "corporate-detial-" + v.contractNo,
                    [],
                    true
                );
            },
            showContractDetailRent(params){
                params.caseType = "RENT";
                this.showContractDetail(params);
            },
            showContractDetailBefore(params){
                params.caseType = "BEFORE_CHARGE";
                this.showContractDetail(params);
            },
            showContractDetail(params) {
                params.viewModel = false;
                params.dealModel = false;
                params.editModel = true;
                params.contractNo = params.contractNo;
                params.caseNo = "";
                params.id = "";
                this.afs.newTab(
                    this,
                    "projects/basic/pages/contract/corporate/index",
                    "对公收款详情",
                    "ios-add",
                    params,
                    "corporate-detial-" + params.contractNo,
                    [],
                    true
                );
            },
            afterHandle(){
                this.getCorporateList();
            },
            deleteCorporate(v){
                let params = {
                    id:v.id
                };
                deleteCorporateTransfer(params).then(res => {
                    if (res.code === "0000") {
                        this.$Message.success(res.data);
                        this.getCorporateList();
                    }
                });
            },
            getCorporateList() {
                // 多条件搜索任务列表
                this.loading = true;
                queryCorporateList(this.searchForm).then(res => {
                    this.loading = false;
                    if (res.code === "0000") {
                        this.data = res.data.records;
                        this.total = res.data.total;
                    }
                });
                this.selectCount=0;
            },
            addCorporateOfBefore(v){
                this.modalBefore = true;
            },
            init(){
                this.searchForm.pageNumber = 1;
                this.searchForm.pageSize = 10;
                this.getCorporateList();
            },
        },
        activated() {
            this.initDataDic();
            this.init();
        }
    }
</script>

<style scoped>

</style>
